package edu.tju.insurance;


import java.sql.SQLException;
import java.util.Date;
import java.util.List;


import org.infohazard.maverick.flow.ControllerContext;

import com.younker.waf.maverick.DispatchCtl;

import edu.tju.model.IStudentDao;
import edu.tju.model.IbatisStudentDao;
import edu.tju.model.Student;




public class InsuranceSetAction extends DispatchCtl{
	
	private final static String SUCCESS_INSERT = "success_insert";
	private final static String SUCCESS_DELETE = "success_delete";
	private final static String SUCCESS_GET_INSURANCE = "success_get_insurance";
	private final static String ERROR_FIND = "error_find";	
	private final static String SUCCESS_GET_INSURANCE_FOR_CHECK = "success_get_insurance_for_check";
	@Override
	protected Object makeFormBean(ControllerContext arg0) {
		
		return new InsuranceSetAction();
	}
	private long id;

    private String name;
    
	private String remark;
	
	private String fee;
	
	private String studentNo;
	
	
	
	

	

	public String getStudentNo() {
		return studentNo;
	}
	public void setStudentNo(String studentNo) {
		this.studentNo = studentNo;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public String getFee() {
		return fee;
	}
	public void setFee(String fee) {
		this.fee = fee;
	}

	IInsuranceDao insuranceDao=new IbatisInsuranceDao();
	public  String insertInsurance(Object formBean, ControllerContext ctx)throws SQLException {
		
		InsuranceSetAction bean = (InsuranceSetAction) formBean;
		Insurance insurance=new Insurance();
		insurance.setName(bean.getName());
		insurance.setFee(bean.getFee());
		insurance.setRemark(bean.getRemark());
		
		insuranceDao.insertInsurance(insurance);

		
		ctx.setModel(insurance);

			return SUCCESS_INSERT;
		}
	
	
	
    public String deleteInsurance(Object formBean, ControllerContext ctx)throws SQLException {
    	InsuranceSetAction bean = (InsuranceSetAction) formBean;
    	insuranceDao.deleteInsurance(bean.getId());
		return SUCCESS_DELETE;
    	
    	
    }
    
    public String getInsuranceById(Object formBean, ControllerContext ctx)throws SQLException {
    	InsuranceSetAction bean = (InsuranceSetAction) formBean;
    	Insurance insurance= insuranceDao.getInsuranceById(bean.getId());
    	ctx.setModel(insurance);
		return SUCCESS_GET_INSURANCE;
    	
    }
    
    public String getInsuranceForCheck(Object formBean, ControllerContext ctx)throws SQLException {
    	InsuranceSetAction bean = (InsuranceSetAction) formBean;
    	Insurance insurance= insuranceDao.getInsuranceById(bean.getId());
    	ctx.setModel(insurance);
		return SUCCESS_GET_INSURANCE_FOR_CHECK;
    	
    }
    
    
    IStudentDao studentDao=new IbatisStudentDao();
    IStudentInsuranceDao studentInsuranceDao=new IbatisStudentInsuranceDao();
    public String addStudentInsurance(Object formBean, ControllerContext ctx)throws SQLException {
    	InsuranceSetAction bean = (InsuranceSetAction) formBean;
    	Student student =new Student();
    	student.setStudentNo(bean.getStudentNo());
    	
    	
    	
    	Date date=new Date();
    	List<Student> students=studentDao.getStudentAnd(student);
    	
    	if(students.size()==0){
    	ctx.getRequest().setAttribute(RET_CODE, RC_FAILURE);
    		
    		return  ERROR_FIND;	
    	}
    	Student stu=students.get(0);
    	
    	StudentInsurance studentInsurance=new StudentInsurance();
    	studentInsurance.setInsuranceId(bean.getId());
    	studentInsurance.setStudentId(stu.getId());
    	studentInsurance.setSetTime(date);
    	studentInsuranceDao.insertStudentInsurance(studentInsurance);
    	
    	
    	ctx.getRequest().setAttribute(RET_CODE, RC_SUCCESS);
		return getInsuranceById(formBean, ctx);
    	
    }
	
}
	
